package cn.yunhe.servlet.FboardSboard;

import cn.yunhe.entity.Sboard;
import cn.yunhe.service.FboardService;
import cn.yunhe.service.SboardService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/productFirstClass2DeleteServlet")
public class productFirstClass2DeleteServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //s1:参数接收
        String fbid2Str=request.getParameter("fbid");
        System.out.println("<===fbid2Str====>"+fbid2Str);
        int fbid=Integer.parseInt(fbid2Str);
        //s2:
        FboardService fboardService=new FboardService();
        SboardService sboardService=new SboardService();
        //s3:删除一版块下的所有二级版块
        List<Sboard> sboardList=sboardService.findSboardByFbid(fbid);
        System.out.println("<====sboardList.size()=====>"+sboardList.size());
        boolean falg2Sboard=false;
        if (sboardList.size()>0){
            for (Sboard sboard:sboardList){
                falg2Sboard=sboardService.deleteSboard(sboard.getSbid());
            }
        }
        //s4:删除一级版块
        Boolean flag2Fboard=fboardService.deleteFboard(fbid);

        if (falg2Sboard&&flag2Fboard){
            System.out.println("<===删除成功==>");

        }else {
            System.out.println("<====删除失败====>");
        }
        response.sendRedirect("secondBoardListServlet");

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
